<template>
<div style="width: 100%">
  <a-card class="card" :bordered="false">
    <div class="table-page-search-wrapper">
      <a-form layout="vertical">
        <a-row :gutter="48">
          <a-col :sm="{span:12}" :lg="{span:12}" :xl="{span:6}" >
            <a-form-item label="院系">
              <a-select :disabled="selectMenu.yx.length === 1" :allowClear="true" showSearch optionFilterProp="children" v-model="queryParam.ssyxm" @change="yxChange">
                <a-select-option v-for="i in selectMenu.yx" :key="i.id" :value="i.dwh">{{i.dwh+"-"+  i.dwmc }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :sm="{span:24}" :lg="{span:12}" :xl="{span:6}">
            <a-form-item label="招生年度">
              <a-select :allowClear="true" showSearch optionFilterProp="children" v-model="queryParam.zsxn">
                <a-select-option v-for="i in TYPE0126" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :sm="{span:24}" style="text-align:center">
            <a-button type="primary" @click="loadData">查询</a-button>
            <a-button type="primary" @click="handleExport">导出</a-button>
            <a-button type="primary" @click="handleExportPdf">导出pdf</a-button>
          </a-col>
        </a-row>
      </a-form>
    </div>
    </a-card>
  <a-card class="card" :bordered="false">
    <a-table
      ref="table"
      size="default"
      rowKey="key"
      :columns="columns"
      :loading="loading"
      :dataSource="data"
      :pagination="pagination"
      @change="handleTableChange"
    >
      <template slot="action" slot-scope="text,record">
        <a @click="handlePreview(record.id)">查看明细</a>
      </template>

      <span slot="serial" slot-scope="text, record, index">
        {{ index + 1 }}
      </span>
    </a-table><!-- 编辑 -->
    <query-detail @close="handlePreviewClose" :info="preViewData"  :previewVisible="previewVisible"></query-detail>
  </a-card>
</div>
</template>

<script>
// import moment from 'moment'
import QueryDetail from './queryDetail'
import { STable } from '~'
import { mapState } from 'vuex'

const selectArr = ['TYPE0126', 'TYPE0075', 'TYPE0125', 'TYPE0072']

export default {
  components: {
    STable,
    QueryDetail
  },
  data () {
    return {
      loading: false,
      data: [],
      // 查询参数
      queryParam: {
        ssyxm: '',
        zsxn:''
      },
      postParams: {},
      // 表头
      columns: [
        {
          title: '序号',
          scopedSlots: { customRender: 'serial' }
        },
        {
          title: '专业',
          dataIndex: 'sszym'
        },
        {
          title: '招生人数',
          dataIndex: 'zsrs'
        },
        {
          title: '考试科目',
          dataIndex: 'kskm'
        },
        {
          title: '复试科目',
          dataIndex: 'fskm'
        },
        {
          title: '加试科目',
          dataIndex: 'jskm'
        },
        {
          title: '备注',
          dataIndex: 'note'
        },
        {
          title: '操作',
          dataIndex: 'cz',
          fixed: 'right',
          width: 120,
          scopedSlots: { customRender: 'action' }
        }
      ],
      previewVisible: false,
      selectMenu: {
        yx: [] // 院系
      },
      preViewData: {},
      pagination: {
        current: 1
      }
    }
  },
  computed: {
    ...mapState({
      systemInfo: state => state.app.systemInfo,
      TYPE0126: state => state.app['TYPE0126'] // 年级
    })
  },
  created () {
    this.initSelectMenu()
    selectArr.forEach(v => {
      this.$store.dispatch('app/setDictionary', v)
    })
  },
  methods: {
    initSelectMenu () {
      let allRequest = [
        this.$api.base.collegeAll()
      ]
      Promise.all(allRequest).then(res => {
        this.selectMenu = {
          yx: res[0].data
        }
        this.queryParam.ssyxm = res[0].data[0].dwh
         // 获取当前学年
        this.$api.base.getSchoolYear().then(res=>{
          this.queryParam.zsxn = res.data.masterAdmissionYear
           this.loadData({
            pageSize: 10,
            pageNum: 1
          })
        })
      })
    },
      // 加载数据
    loadData(parameter = {}) {
      if(!parameter.pageNum){
        this.pagination.current = 1
        parameter.pageNum = 1
      }
      let params = Object.assign(this.queryParam, parameter)
      this.loading = true;
      this.$api.recruitstat.memuCheckList(params).then(res => {
        // console.log(res)
         const pagination = { ...this.pagination
        };
        pagination.total = 0
        if (res.data && res.data.total) {
          pagination.total = res.data.total;
        }
        this.data = []
        this.pagination = pagination;
        if (res.data && res.data.rows.length) {
          this.data = res.data.rows;
        }
        this.loading = false;
      })
    },
    // 分页
    handleTableChange(pagination, filters, sorter) {
      console.log(pagination);
      const pager = { ...this.pagination
      };
      pager.current = pagination.current;
      this.pagination = pager;
      this.loadData({
        pageSize: 10,
        pageNum: pagination.current
      });
    },
    // 选择院系
    yxChange (code) {
    },
    // 查看明细
    handlePreview (key) {
      this.$api.recruitstat.memuCheckId(key).then( res => {
         this.preViewData = res.data
         this.previewVisible = true
      })
    },
    // 关闭查看明细弹层
    handlePreviewClose () {
      this.previewVisible = false
      this.loadData({
        pageSize:10,
        pageNum:this.pagination.current
      })
    },
    // 导出
    handleExport () {
      console.log('导出')
      this.$export('admissions/zsmlcx/export', this.queryParam)
    },
    handleExportPdf(){
      this.$export('admissions/zsmlcx/exportPDF', this.queryParam)
    }
  }
}
</script>

<style lang="less" scoped>
.card{
  margin-bottom: 20px;
}
</style>
